'use strict';

const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const { app } = require('electron');

/**
 * example
 * @class
 */
class NetworkController extends Controller {

    constructor(ctx) {
        super(ctx);
    }


    /**
     * 所有方法接收两个参数
     * @param args 前端传的参数
     * @param event - ipc通信时才有值。详情见：控制器文档
     */

    /**
     * test
     */
    async test () {
        const result = await Services.get('example').test('electron');
        Log.info('service result:', result);

        return 'hello electron-egg';
    }

    async getVersion () {
        return app.getVersion();
    }


    async getIp(interfaceName){
        const result = await Services.get('network').getIPv4Address(interfaceName);
        Log.info('service result:', result);
        return result;
    }

    /**
    * 查询所有网卡
    */
    async getAllNetworkInterfaces(){
        return await Services.get('network').getAllNetworkInterfaces();
    }

    /**
     * 设置IP
     */
    async setIPAddress(args){
        const {interfaceName, classRoom} = args
        Log.info('service result:', interfaceName, classRoom);
        return await Services.get('network').setIPAddress(interfaceName, classRoom);
    }

    /**
     * 设置DHCP
     */
    async setIPDhcp(args){
        const {interfaceName} = args
        Log.info('service result:', interfaceName);
        return await Services.get('network').setIPDhcp(interfaceName);
    }


    /**
     * json数据库操作
     */
    async jsondbOperation(args) {
        const { action, info, delete_name, update_name, update_arr, search_age, data_dir } = args;
        Log.info('service result:', args);
        const data = {
            action,
            result: null,
            all_list: []
        };

        switch (action) {
            case 'add' :
                data.result = await Services.get('jsondb').addTestData(info);
                break;
            case 'del' :
                data.result = await Services.get('jsondb').delTestData(delete_name);
                break;
            case 'update' :
                data.result = await Services.get('jsondb').updateTestData(update_name, update_arr);
                break;
            case 'get' :
                data.result = await Services.get('jsondb').getTestData(search_age);
                break;
            case 'getDataDir' :
                data.result = await Services.get('jsondb').getDataDir();
                break;
            case 'setDataDir' :
                data.result = await Services.get('jsondb').setCustomDataDir(data_dir);
                break;
        }

        data.all_list = await Services.get('jsondb').getAllTestData();

        return data;
    }

}

NetworkController.toString = () => '[class NetworkController]';
module.exports = NetworkController;